package TCP.moreSent;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
/*
* 多发多收
* */
public class TCPClientDemo1 {
    public static void main(String[] args) throws Exception {
        System.out.println("=====客户端启动=====");
        //1、创建socket对象，作为客户端，要连接的服务器的地址和端口号
        Socket socket = new Socket("localhost", 8888);
        //键盘输入数据
        Scanner sc = new Scanner(System.in);
        OutputStream os = socket.getOutputStream();
        while (true){
            System.out.println("请输入数据:");
            String msg = sc.next();
            if ("886".equals(msg)){
                System.out.println("=====客户端退出=====");
                socket.close();
                sc.close();
                os.close();
                break;
            }
            //2、获取输出流，发送数据
            os.write(msg.getBytes());
            os.flush();//养成习惯

            //3、接收服务器返回的数据
            InputStream is = socket.getInputStream();
            byte[] bytes = new byte[1024];
            int len = is.read(bytes);
            System.out.println("服务器返回:" + new String(bytes, 0, len));

        }
    }
}
